/*
 * example_01.cpp
 *
 *  Created on: Jan 25, 2014
 *      Author: filipe
 */
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include "mlp.h"

using namespace std;


vector<double> vec_x;
vector<double> vec_y;

double **data_x;
double **data_y;


void
load_data()
{
	double x, y;
	FILE *f = fopen("train.txt", "r");

	if (f == NULL)
		exit(printf("'train.txt' not exists\n"));

	while(!feof(f))
	{
		if (fscanf(f, "%lf %lf", &x, &y) == 2)
		{
			vec_x.push_back(x);
			vec_y.push_back(y);
		}
	}

	data_x = (double**) calloc (vec_x.size(), sizeof(double*));
	data_y = (double**) calloc (vec_y.size(), sizeof(double*));

	for (unsigned int i = 0; i < vec_x.size(); i++)
	{
		data_x[i] = (double*) calloc (1, sizeof(double));
		data_y[i] = (double*) calloc (1, sizeof(double));

		data_x[i][0] = vec_x[i];
		data_y[i][0] = vec_y[i];
	}

	fclose(f);
}


void
show_net_output(double **x, double **y, int num_examples)
{
	int i;

	for (i = 0; i < num_examples; i++)
	{
		printf("%lf %lf\n", x[i][0], y[i][0]);
	}
}


int
main(int argc, char **argv)
{
	int data_dim = 1;
	int num_layers = 3;
	int num_neurons_per_layer[] = {20, 20, 1};

	load_data();

	mlp_initialize_config
	(
		num_layers,
		num_neurons_per_layer,
		data_dim,
		0.1,
		10000,
		0.1,
		false
	);

	mlp_build_netowrk();
	mlp_train(data_x, data_y, vec_x.size());
	// mlp_print();
	mlp_test(data_x, data_y, vec_x.size());
	mlp_free();

	show_net_output(data_x, data_y, vec_x.size());

	return 0;
}



